package com.net.tkc.pro.tool;

import org.springframework.stereotype.Component;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.File;
import java.util.Properties;

@Component//注入Springboot框架
public class EmailTool {
    /**
     * 邮件发送
     * @param descMail 收件人地址
     * @param title 邮件标题
     * @param Content 邮件内容
     * @param files    邮件附件
     * @return boolean :True:发送成功，False:发送失败
     */
    public boolean sendMail(String descMail, String title, String Content, File[] files){
        final String fromEmail="2821819113@qq.com";//发件人邮箱地址
        final String smtpCode="lrwygvsbhstedfje";//发件人授权码
        boolean flag =true;//判定是否发送成功！
        String ContentType="text/html;charset=utf-8";//请求头
        Properties properties = new Properties();//设置邮箱属性的对象
        properties.setProperty("mail.smtp.host","smtp.qq.com");//设置邮箱服务器的地址
        properties.setProperty("mail.smtp.port","465");//设置邮箱发送端口
        properties.setProperty("mail.smtp.auth","true");//是否加密
        properties.setProperty("mail.debug","true");//是否开启调试
        properties.setProperty("mail.transport.protocol","smtp");//发送形式
        properties.setProperty("mail.smtp.ssl.enable","true");//是否开启HTTPS
        Session session = Session.getInstance(properties, new Authenticator() {//获取加密的请求头
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(fromEmail,smtpCode);
            }
        });
        Message message = new MimeMessage(session);//生成邮件发送的消息对象
        try{
            message.setFrom( new InternetAddress(fromEmail));//设置发件人邮件地址
            message.setRecipient(Message.RecipientType.TO,new InternetAddress(descMail));//设置收件人地址
            message.setSubject(title);//设置邮件标题
            if(files !=null && files.length>0){//有附件
                MimeMultipart mimeMultipart = new MimeMultipart();//文件上传对象
                BodyPart bodyPart = new MimeBodyPart();//请求主体
                bodyPart.setContent(Content,ContentType);
                mimeMultipart.addBodyPart(bodyPart);
                for(File file:files){
                    MimeBodyPart mimeBodyPart = new MimeBodyPart();
                    DataHandler dh = new DataHandler(new FileDataSource(file));//将文件转换为表单形式
                    mimeBodyPart.setDataHandler(dh);
                    mimeBodyPart.setFileName(MimeUtility.encodeText(dh.getName()));//获取文件名
                    mimeMultipart.addBodyPart(mimeBodyPart);//将文件添加到请求主体
                }
                mimeMultipart.setSubType("mixed");
                message.setContent(mimeMultipart);//将请求添加到消息中
                message.saveChanges();//消息保存修改
            }else{//普通邮件
                message.setContent(Content,ContentType);
            }
            Transport transport = session.getTransport();//连接到邮箱服务器
            transport.connect(fromEmail,smtpCode);
            Transport.send(message);//发送邮件
        }catch (Exception e){
            e.printStackTrace();
            flag=false;
        }
        return flag;
    }
}
